<html>
<head><meta charset="utf-8"><title>Using const unreachable in libcore · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html">Using const unreachable in libcore</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210642572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210642572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210642572">(Sep 20 2020 at 00:46)</a>:</h4>
<p>I'm trying to use <code>unreachable</code> intrinsics in a method in core but it's already a const-fn. So I tried to enable the features for it, <code>const_fn</code> and <code>const_unreachable_unchecked</code>, like it's telling me to but it doesn't work. The PR for that feature merged Jul-16 so that should be in beta, right?</p>



<a name="210645680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210645680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210645680">(Sep 20 2020 at 02:35)</a>:</h4>
<p><span class="user-mention" data-user-id="229913">@HeroicKatora</span> it works for me with <code>const_panic</code>: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=1e56a15ad80f988d72ee167a7d75b9e5">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=1e56a15ad80f988d72ee167a7d75b9e5</a></p>



<a name="210645682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210645682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210645682">(Sep 20 2020 at 02:35)</a>:</h4>
<p>why'd you enable <code>const_unreachable_unchecked</code>, did the diagnostic tell you to? That might be a diagnostic bug</p>



<a name="210647176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210647176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210647176">(Sep 20 2020 at 03:28)</a>:</h4>
<p>Clarification, diagnostics only tell me to enable <code>const_fn</code>. Enabling the other was me trying figure if it was a diagnostic bug</p>



<a name="210647184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210647184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210647184">(Sep 20 2020 at 03:29)</a>:</h4>
<p>And this is for stage0 compilation in <code>core</code> specifically. Is there any difference to a standard compilation?</p>



<a name="210647786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210647786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210647786">(Sep 20 2020 at 03:50)</a>:</h4>
<p>not really for core, it's compiling with beta</p>



<a name="210647790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210647790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210647790">(Sep 20 2020 at 03:50)</a>:</h4>
<p>it suggested <code>const_panic</code> on playground though: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=60177a1d5891157ab62302f2ca05ef7f">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=60177a1d5891157ab62302f2ca05ef7f</a></p>



<a name="210667189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210667189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210667189">(Sep 20 2020 at 13:29)</a>:</h4>
<p>It seems that nightly suggests it, not beta though. I guess I might just have to wait one beta cycle then?</p>



<a name="210667704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210667704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210667704">(Sep 20 2020 at 13:44)</a>:</h4>
<p>you could use <code>cfg(not(bootstrap))</code></p>



<a name="210673036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210673036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210673036">(Sep 20 2020 at 16:09)</a>:</h4>
<p>Can this be done for stage1 as well? Still getting the same failure there.</p>



<a name="210673352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210673352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210673352">(Sep 20 2020 at 16:16)</a>:</h4>
<p>you might add feature gate in <a href="http://lib.rs">lib.rs</a> and add <code>#[allow_internal_unstable(&lt;feature&gt;)]</code>to the method/function<br>
use it.</p>



<a name="210673633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210673633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210673633">(Sep 20 2020 at 16:23)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="w">    </span><span class="cp">#[allow_internal_unstable(const_fn)]</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[allow_internal_unstable(const_unreachable_unchecked)]</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">len</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// SAFETY: this is safe because `&amp;[T]` and `FatPtr&lt;T&gt;` have the same layout.</span>
<span class="w">        </span><span class="c1">// Only `std` can make this guarantee.</span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">raw_len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">crate</span>::<span class="n">ptr</span>::<span class="n">Repr</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">rust</span>: <span class="nc">self</span><span class="w"> </span><span class="p">}.</span><span class="n">raw</span><span class="p">.</span><span class="n">len</span><span class="w"> </span><span class="p">};</span><span class="w"></span>

<span class="w">        </span><span class="k">match</span><span class="w"> </span><span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="mi">0</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">            </span><span class="c1">// SAFETY: this branch taken if mathematically `isize::MAX &lt; raw_len * N`.</span>
<span class="w">            </span><span class="c1">// But references must point to one allocation with size at most isize::MAX.</span>
<span class="w">            </span><span class="n">N</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="kt">isize</span>::<span class="n">MAX</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">raw_len</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">hint</span>::<span class="n">unreachable_unchecked</span><span class="p">()</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">            </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>

<span class="w">        </span><span class="n">raw_len</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>I have this, it doesn't work.</p>



<a name="210673677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210673677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210673677">(Sep 20 2020 at 16:24)</a>:</h4>
<div class="codehilite"><pre><span></span><code>Building stage1 std artifacts (x86_64-unknown-linux-gnu -&gt; x86_64-unknown-linux-gnu)
   Compiling core v0.0.0 (/home/andreas/code/projects/rust/library/core)
error[E0723]: can only call other `const fn` within a `const fn`, but `unreachable_unchecked` is not stable as `const fn`
   --&gt; library/core/src/slice/mod.rs:108:66
    |
108 |             N if (isize::MAX as usize) / N &lt; raw_len =&gt; unsafe { hint::unreachable_unchecked() },
    |                                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #57563 &lt;https://github.com/rust-lang/rust/issues/57563&gt; for more information
    = help: add `#![feature(const_fn)]` to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0723`.
error: could not compile `core`.
</code></pre></div>



<a name="210673857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210673857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210673857">(Sep 20 2020 at 16:28)</a>:</h4>
<p>Did you add feature(const_unreachable_unchecked) to crate root ?</p>



<a name="210673869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210673869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210673869">(Sep 20 2020 at 16:29)</a>:</h4>
<p>It's already present</p>



<a name="210673883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210673883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210673883">(Sep 20 2020 at 16:29)</a>:</h4>
<p><code>const_fn</code> was missing but I added that.</p>



<a name="210674008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210674008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210674008">(Sep 20 2020 at 16:32)</a>:</h4>
<p>wait, so stage0 builds Ok?</p>



<a name="210674079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210674079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210674079">(Sep 20 2020 at 16:34)</a>:</h4>
<p>Yeah, but only due to <code>cfg(not(bootstrap))</code></p>



<a name="210674248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210674248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210674248">(Sep 20 2020 at 16:39)</a>:</h4>
<p>if you add <code>#![feature(const_panic)]</code> does it work?</p>



<a name="210674281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210674281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210674281">(Sep 20 2020 at 16:40)</a>:</h4>
<p>(might want to turn off cfg(bootstrap) so you get faster feedback)</p>



<a name="210674303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210674303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210674303">(Sep 20 2020 at 16:41)</a>:</h4>
<p>not sure now, probably ping <span class="user-mention" data-user-id="124288">@oli</span> or <span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="210676214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210676214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210676214">(Sep 20 2020 at 17:28)</a>:</h4>
<p><code>allow_internal_unstable</code> is for macros, not functions.</p>



<a name="210679309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210679309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210679309">(Sep 20 2020 at 18:41)</a>:</h4>
<p>There's an existing <code>#[allow_internal_unstable(const_fn_union)]</code> on <code>[T]::len</code>.. <span aria-label="smiley" class="emoji emoji-1f603" role="img" title="smiley">:smiley:</span></p>



<a name="210682197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210682197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210682197">(Sep 20 2020 at 19:53)</a>:</h4>
<p>It's meant for code inside a macro expansion to use unstable features without the crate using the macro needing to declare those features.</p>



<a name="210688954"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210688954" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210688954">(Sep 20 2020 at 22:42)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore/near/210676214">said</a>:</p>
<blockquote>
<p><code>allow_internal_unstable</code> is for macros, not functions.</p>
</blockquote>
<p>it's also for <code>const fn</code></p>



<a name="210698080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210698080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210698080">(Sep 21 2020 at 03:13)</a>:</h4>
<p>You could introduce a macro to wrap unreachable_unchecked and add allow_internal_unstable to that macro.</p>



<a name="210698081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210698081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210698081">(Sep 21 2020 at 03:13)</a>:</h4>
<p>That's tedious but it works.</p>



<a name="210708549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210708549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210708549">(Sep 21 2020 at 07:27)</a>:</h4>
<p>but it still shouldn't help for the case of a libcore <code>const fn</code> then using that macro</p>



<a name="210708594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210708594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210708594">(Sep 21 2020 at 07:28)</a>:</h4>
<p>I think <span class="user-mention" data-user-id="143274">@Amanieu</span> just was not aware that const stability checking also uses <code>allow_internal_unstable</code></p>



<a name="210708639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210708639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210708639">(Sep 21 2020 at 07:29)</a>:</h4>
<p>we are more paranoid for const-stability than for regular stability because it is harder to hide implementation details of a <code>const fn</code> in a semver-compatible way -- any change in the function's behavior could break compilation of something</p>



<a name="210712185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210712185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210712185">(Sep 21 2020 at 08:16)</a>:</h4>
<p>So I just wait? I'm fine waiting a release cycle or two here. This is probably also relevant to a missed optimization on NonZero which wants to use a similar trick but currently gravitates towards using <code>assume</code> as far as I can see.</p>



<a name="210723185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210723185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210723185">(Sep 21 2020 at 10:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore/near/210708549">said</a>:</p>
<blockquote>
<p>but it still shouldn't help for the case of a libcore <code>const fn</code> then using that macro</p>
</blockquote>
<p>./x.py check passed locally for me.</p>



<a name="210754072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210754072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210754072">(Sep 21 2020 at 15:02)</a>:</h4>
<p><span class="user-mention" data-user-id="209117">@lzutao</span> Would you mind sharing the specific code that works for you?</p>



<a name="210755422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210755422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210755422">(Sep 21 2020 at 15:12)</a>:</h4>
<p><span class="user-mention" data-user-id="229913">@HeroicKatora</span> Here. I've not run it to stage1 but it passed x.py check</p>
<div class="spoiler-block"><div class="spoiler-header">

</div><div class="spoiler-content" aria-hidden="true">

<div class="codehilite"><pre><span></span><code><span class="gh">diff --git library/core/src/slice/mod.rs library/core/src/slice/mod.rs</span>
<span class="gh">index ff926c517bc..3fbf6d1e3f8 100644</span>
<span class="gd">--- library/core/src/slice/mod.rs</span>
<span class="gi">+++ library/core/src/slice/mod.rs</span>
<span class="gu">@@ -75,6 +75,11 @@ pub use index::SliceIndex;</span>
 #[unstable(feature = &quot;slice_check_range&quot;, issue = &quot;76393&quot;)]
 pub use index::check_range;

<span class="gi">+#[allow_internal_unstable(const_unreachable_unchecked)]</span>
<span class="gi">+macro_rules! unreachable_unchecked {</span>
<span class="gi">+    () =&gt; { $crate::hint::unreachable_unchecked() };</span>
<span class="gi">+}</span>
<span class="gi">+</span>
 #[lang = &quot;slice&quot;]
 #[cfg(not(test))]
 impl&lt;T&gt; [T] {
<span class="gu">@@ -94,7 +99,12 @@ impl&lt;T&gt; [T] {</span>
     pub const fn len(&amp;self) -&gt; usize {
         // SAFETY: this is safe because `&amp;[T]` and `FatPtr&lt;T&gt;` have the same layout.
         // Only `std` can make this guarantee.
<span class="gd">-        unsafe { crate::ptr::Repr { rust: self }.raw.len }</span>
<span class="gi">+        let raw_len = unsafe { crate::ptr::Repr { rust: self }.raw.len };</span>
<span class="gi">+        match mem::size_of::&lt;T&gt;() {</span>
<span class="gi">+            0 =&gt; raw_len,</span>
<span class="gi">+            n if raw_len * n &gt; (isize::MAX as usize) =&gt; unsafe { unreachable_unchecked!() },</span>
<span class="gi">+            _ =&gt; raw_len,</span>
<span class="gi">+        }</span>
     }

     /// Returns `true` if the slice has a length of 0.
</code></pre></div>


</div></div>



<a name="210755724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210755724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210755724">(Sep 21 2020 at 15:14)</a>:</h4>
<p>Please don't add such a macro and just import the function directly -- macros for <em>unsafe</em> code when they're just calling a single function seems like an anti-pattern</p>



<a name="210755929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210755929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210755929">(Sep 21 2020 at 15:16)</a>:</h4>
<p>the problem is we (I and <span class="user-mention" data-user-id="229913">@HeroicKatora</span> ) cannot find a way to satisfy the const stability checker when using unstable <br>
<code>core::hint::unreachable_unchecked</code> in const fn <code>slice::len</code>.</p>



<a name="210756442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210756442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210756442">(Sep 21 2020 at 15:18)</a>:</h4>
<p>If we could, I don't end up using this ugly hack.</p>



<a name="210758474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210758474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210758474">(Sep 21 2020 at 15:33)</a>:</h4>
<p>the macro has an effect on const stability checking?</p>



<a name="210758653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210758653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210758653">(Sep 21 2020 at 15:34)</a>:</h4>
<p>Based on what Ralf said, it sounds like we should be able to "just" have a allow_internal_unstable on the [T]::len</p>



<a name="210760529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210760529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210760529">(Sep 21 2020 at 15:48)</a>:</h4>
<p>It sadly doesn't work as intended: <a href="#narrow/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore/near/210673352">https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore/near/210673352</a></p>



<a name="210774385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210774385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210774385">(Sep 21 2020 at 17:35)</a>:</h4>
<p>hm, maybe a beta bug, I guess</p>



<a name="210851793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/210851793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#210851793">(Sep 22 2020 at 10:26)</a>:</h4>
<p>Seems like it works if the annotations are merged into a single one. Not sure if one of the const-check changes (i.e. 76807) is responsible or if this worked yesterday. In any case, fmt or checks should probably complain if there is more than one <code>#[allow_internal_unstable]</code> attribute.</p>



<a name="211157956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211157956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211157956">(Sep 24 2020 at 16:42)</a>:</h4>
<p>I think more than one <code>allow_internal_unstable</code> is just not supported and so far nobody tried that^^</p>



<a name="211158212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211158212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211158212">(Sep 24 2020 at 16:44)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/77088">#77088</a> has mentor instructions if someone wants to implement it.</p>



<a name="211162522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211162522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211162522">(Sep 24 2020 at 17:19)</a>:</h4>
<p>I can take a look at it this weekend, unless someone else does it faster <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="211243696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211243696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211243696">(Sep 25 2020 at 11:03)</a>:</h4>
<p>allow_internal_unstable is documented in source as only for macro_rules! ... Maybe when it was extended for const fn, that fact was not documented?</p>



<a name="211252758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211252758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211252758">(Sep 25 2020 at 12:45)</a>:</h4>
<p>I opened a PR (<a href="https://github.com/rust-lang/rust/issues/77183">#77183</a>) that fixes <a href="https://github.com/rust-lang/rust/issues/77088">#77088</a>.</p>



<a name="211353475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211353475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211353475">(Sep 26 2020 at 10:21)</a>:</h4>
<p><span class="user-mention silent" data-user-id="334260">Dániel Buga</span> <a href="#narrow/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore/near/211243696">said</a>:</p>
<blockquote>
<p>allow_internal_unstable is documented in source as only for macro_rules! ... Maybe when it was extended for const fn, that fact was not documented?</p>
</blockquote>
<p>yeah I think that is what happened... Cc <span class="user-group-mention" data-user-group-id="1916">@WG-const-eval</span></p>



<a name="211355391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211355391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211355391">(Sep 26 2020 at 11:16)</a>:</h4>
<p>I also think it would be good to also enable allow_internal_unstable for non-const functions as well. Even if it will be properly documented, I think there could be some confusion about why it isn't. But that's just my .02€</p>



<a name="211359318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211359318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211359318">(Sep 26 2020 at 13:08)</a>:</h4>
<p>which effect should it have on non-const fn though?</p>



<a name="211359353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211359353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211359353">(Sep 26 2020 at 13:08)</a>:</h4>
<p>the reason we need it on stable const fn is that, unlike stable non-const fn, we have a check which makes sure that stable const fn only call stable const fn.</p>



<a name="211359357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211359357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211359357">(Sep 26 2020 at 13:08)</a>:</h4>
<p>so we need the attribute to be able to suppress that check</p>



<a name="211359638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211359638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211359638">(Sep 26 2020 at 13:16)</a>:</h4>
<p>So <code>const fn</code> ignore any other feature gates (at least lifted via allow_internal_unstable) anyway? I must admit, I don't get some things yet <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="211360434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211360434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211360434">(Sep 26 2020 at 13:37)</a>:</h4>
<p><em>stable</em> <code>const fn</code> basically ignore "outer" feature gates of the lib they are defined in, yes. that is a way to put it.</p>



<a name="211360479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211360479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211360479">(Sep 26 2020 at 13:38)</a>:</h4>
<p>also see <a href="https://github.com/rust-lang/rust/pull/76850">https://github.com/rust-lang/rust/pull/76850</a></p>



<a name="211360606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Using%20const%20unreachable%20in%20libcore/near/211360606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Using.20const.20unreachable.20in.20libcore.html#211360606">(Sep 26 2020 at 13:40)</a>:</h4>
<p>Thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>